package com.extension.company.view.home.hotactivity;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.extension.company.admin.R;
import com.extension.company.common.mvp.BaseTitleActivity;
import com.extension.company.common.widget.ShapedImageView;
import com.extension.company.contract.home.OnlineWaitForPaymentContract;
import com.extension.company.model.entity.AddressListData;
import com.extension.company.model.entity.OrderConfirmCarListBean;
import com.extension.company.presenter.home.OnlineWaitForPayActivityPresenter;
import com.extension.company.view.my.address.AddressListActivity;
import com.wuhenzhizao.sku.bean.Sku;
import com.wuhenzhizao.sku.bean.SkuAttribute;

import butterknife.BindView;
import butterknife.OnClick;

import static com.extension.company.view.my.address.AddressListActivity.ADDRESS_RESULT;

/**
 * create:zwk
 * data:20192/28
 * 功能:订单确认页
 */
public class OnlineWaitForPayActivity extends BaseTitleActivity<OnlineWaitForPaymentContract.View, OnlineWaitForPayActivityPresenter>
		implements OnlineWaitForPaymentContract.View {
	@BindView(R.id.order_submit)
	Button orderSubmit;
	@BindView(R.id.recerver_address)
	RelativeLayout recerverAddress;
	@BindView(R.id.consignee)
	TextView consignee;
	@BindView(R.id.consignee_phone)
	TextView consigneePhone;
	@BindView(R.id.address)
	TextView address;
	@BindView(R.id.order_confirm_store_logo)
	ImageView orderConfirmStoreLogo;
	@BindView(R.id.order_confirm_store_name)
	TextView orderConfirmStoreName;
	@BindView(R.id.order_comfirm_goods_img)
	ShapedImageView orderComfirmGoodsImg;
	@BindView(R.id.price)
	TextView price;
	@BindView(R.id.order_comfirm_goods_name)
	TextView orderComfirmGoodsName;
	@BindView(R.id.order_comfirm_goods_sku)
	TextView orderComfirmGoodsSku;
	@BindView(R.id.free_ship)
	TextView freeShip;
	@BindView(R.id.order_confirm_quantity)
	TextView orderConfirmQuantity;
	@BindView(R.id.order_confirm_total)
	TextView orderConfirmTotal;
	@BindView(R.id.order_confirm_total_price)
	TextView orderConfirmTotalPrice;
	public final static int WFPY = 0x121;
	private String goodsId; // 查询结果的订单的信息
	private Sku sku;
	private int orderGoodsNum;// 购买的数量
	private float totalPrice;
	private int addressId; // 地址的id

	@Override
	public int getMainLayout() {
		return R.layout.activity_wait_for_payment;
	}

	@Override
	public OnlineWaitForPaymentContract.View createView() {
		return this;
	}

	@Override
	public OnlineWaitForPayActivityPresenter createPresenter() {
		return new OnlineWaitForPayActivityPresenter();
	}

	@Override
	public void initData() {
		this.sku = getIntent().getParcelableExtra("orderInfo");
		this.orderGoodsNum = getIntent().getIntExtra("orderNum", 0);
		initIntent();
		initView();
		initHttp();
	}

	private void initIntent() {
		goodsId = getIntent().getStringExtra("goods_Info_Id");
	}


	private void initView() {
		setTitle("待付款");
		if (null != sku) {
			orderComfirmGoodsName.setText(sku.getGoodsName());
			StringBuilder builder = new StringBuilder();
			if (null != sku.getAttributes()) {
				for (SkuAttribute skuAttribute : sku.getAttributes()) {
					builder.append(skuAttribute.getKey())
							.append(":")
							.append(skuAttribute.getValue())
							.append(";");
				}
			}
			orderComfirmGoodsSku.setText(builder.toString());
			price.setText(String.format(getResources().getString(R.string.online_wait_pay_money), String.valueOf(sku.getSellingPrice())));
			Glide.with(this).load(sku.getMainImage()).into(orderComfirmGoodsImg);
			orderConfirmQuantity.setText(String.format(getResources().getString(R.string.order_confirm_quantity), orderGoodsNum));
			totalPrice = Float.valueOf(orderGoodsNum * sku.getSellingPrice());
			orderConfirmTotal.setText(String.format(getResources().getString(R.string.order_confirm_small_total), totalPrice));
			orderConfirmTotalPrice.setText("" + totalPrice);
		}
	}

	private void initHttp() {
//		presenter.getDefaultAddress();
//		getDefaultAddress();
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append("a")
				.append(sku.getId())
				.append("-b")
				.append(sku.getGoodsSpecSkuId())
				.append("-c")
				.append(orderGoodsNum);

		presenter.orderConfirmCarList(stringBuilder.toString(), ""); // 第二个参数传空
	}


	@Override
	public void initListener() {

	}

	@OnClick({R.id.order_submit, R.id.recerver_address})
	public void onClick(View view) {
		switch (view.getId()) {
			case R.id.order_submit:
				presenter.payForOrder(this, "32", "54", "1");
				break;
			case R.id.recerver_address: // 跳转到添加地址界面
				Intent intent1 = new Intent(OnlineWaitForPayActivity.this, AddressListActivity.class);
				intent1.putExtra("OnlineWaitForPayActivity", true);
				startActivityForResult(intent1, WFPY);
				break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == WFPY && resultCode == ADDRESS_RESULT) {
			AddressListData.DataBean.AddressBean addressBean = data.getParcelableExtra("orderAddress");
			consignee.setText(String.format(getResources().getString(R.string.receiver_people), addressBean.getName()));
			consigneePhone.setText(addressBean.getPhone());
			address.setText(String.format(getResources().getString(R.string.address), addressBean.getPname()
					+ addressBean.getCname() + addressBean.getRname() + addressBean.getDetail()));
			this.addressId = addressBean.getId(); // 获取地址的id
		}
	}

	/**
	 * 获取默认的地址
	 */
	private void getDefaultAddress() {

	}


	@Override
	public void showLoading() {

	}

	@Override
	public void hideLoading() {

	}

	@Override
	public void onError(Throwable throwable) {

	}

	@Override
	public void defaultAddress(String phone, String detailAddress, String receiverName) {  // 默认的地址
		consignee.setText(String.format(getResources().getString(R.string.receiver_people), receiverName));
		consigneePhone.setText(phone);
		address.setText(String.format(getResources().getString(R.string.address), detailAddress));
	}

	@Override
	public void orderId(String orderId) {
		Log.d("zwk", "订单的id" + orderId);
//		EventBus.getDefault().post(new OrderId(orderId));
	}

	@Override
	public void orderDefaultAddress(OrderConfirmCarListBean.DataBeanX.DataBean addressData, boolean hasDefaultAddress) {
		if (hasDefaultAddress) {
			consignee.setText(String.format(getResources().getString(R.string.receiver_people), addressData.getExist_address().getName()));
			consigneePhone.setText(addressData.getExist_address().getPhone());
			address.setText(String.format(getResources().getString(R.string.address), addressData.getExist_address().getArea().getProvince()
					+ addressData.getExist_address().getArea().getCity() + addressData.getExist_address().getArea().getRegion() + addressData.getExist_address().getDetail()));
			freeShip.setText(addressData.getExpress_price() != 0 ? "¥" + addressData.getExpress_price() : "免邮");
		} else {
			consignee.setText(String.format(getResources().getString(R.string.receiver_people), "请选择"));
			consigneePhone.setText("请选择");
			address.setText(String.format(getResources().getString(R.string.address), "请选择"));
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

}
